﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using MyDMS.DAL;

namespace MyDMS_ExtJS
{
    public partial class Folder : PageBase
    {
        MyDMSEntity _myDmsEntity = new MyDMSEntity();
        public string FolderName = string.Empty;
        private int _proj_code, _task_code;
        protected int _dept_code = 0;
        protected int _folder_code = 0;
        protected int _assgin_code = 0;
        protected int _owner_code = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            #region 处理参数
            if (Request.QueryString["proj"] != null)
            {
                _proj_code = int.Parse(Request.QueryString["proj"]);
            }
            if (Request.QueryString["task"] != null)
            {
                _task_code = int.Parse(Request.QueryString["task"]);
                var firstOrDefault = _myDmsEntity.Task.FirstOrDefault(t => t.Task_Code == _task_code);
                if (firstOrDefault != null)
                {
                    _assgin_code = firstOrDefault.Assigned_To;
                }
            }
            if (Request.QueryString["dept"] != null)
            {
                _dept_code = int.Parse(Request.QueryString["dept"]);
            }
            if (Request.QueryString["folder"] != null)
            {
                _folder_code = int.Parse(Request.QueryString["folder"]);
                var firstOrDefault = _myDmsEntity.Folder.FirstOrDefault(t => t.Folder_Code == _folder_code);
                if (firstOrDefault != null)
                {
                    FolderName = firstOrDefault.Folder_Title;
                }
            }

            #endregion

            if (!IsPostBack)
            {
                btnAdd.OnClientClick =
                    UploadWindow.GetShowReference(
                        string.Format("UploadDocument.aspx?dept={0}&proj={1}&task={2}&folder={3}", _dept_code,
                                      _proj_code, _task_code, _folder_code));
            }



            var orDefault = _myDmsEntity.Project.FirstOrDefault(p => p.Project_Code == _proj_code);
            var _isAnony = orDefault != null && orDefault.Is_Anonymous;
            if (Author.department_Code == _dept_code || _isAnony)
            {
                BindgvDocuments();
            }
        }


        private void BindgvDocuments()
        {
            var query = from d in _myDmsEntity.Document.Where(d => d.Folder_Code == _folder_code)
                        select new
                        {
                            d.Document_Code,
                            d.Document_Desc,
                            d.Document_Name,
                            d.Owner,
                            TaskName = _myDmsEntity.Task.FirstOrDefault(t => t.Task_Code == d.Task_Code).Task_Title,
                            FolderName=_myDmsEntity.Folder.FirstOrDefault(t => t.Folder_Code == d.Folder_Code).Folder_Title,
                            OwnerName =
                 _myDmsEntity.User.FirstOrDefault(u => u.User_Code == d.Owner).FirstName +
                 _myDmsEntity.User.FirstOrDefault(u => u.User_Code == d.Owner).LastName,
                            SecretLevelName = _myDmsEntity.SecretLevel.FirstOrDefault(sl => sl.SecretLevel_Code == d.SecretLevel_Code).SecretLevel_Name,
                            ShelfLifeName = _myDmsEntity.ShelfLife.FirstOrDefault(s => s.ShelfLife_Code == d.ShelfLife_Code).ShelfLife_Name,
                            d.Updation_Date
                        };
            DataTable table = query.CopyToDataTable();
            DataView view = table.DefaultView;
            gvDocuments.DataSource = view;
            gvDocuments.DataBind();
        }


        protected void gvDocuments_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            var query = from d in _myDmsEntity.Document.Where(d => d.Folder_Code == _folder_code)
                        select new
                        {
                            d.Document_Code,
                            d.Document_Desc,
                            d.Document_Name,
                            d.Owner,
                            TaskName = _myDmsEntity.Task.FirstOrDefault(t => t.Task_Code == d.Task_Code).Task_Title,
                            FolderName = _myDmsEntity.Folder.FirstOrDefault(t => t.Folder_Code == d.Folder_Code).Folder_Title,
                            OwnerName =
                 _myDmsEntity.User.FirstOrDefault(u => u.User_Code == d.Owner).FirstName +
                 _myDmsEntity.User.FirstOrDefault(u => u.User_Code == d.Owner).LastName,
                                            SecretLevelName = _myDmsEntity.SecretLevel.FirstOrDefault(sl => sl.SecretLevel_Code == d.SecretLevel_Code).SecretLevel_Name,
                                            ShelfLifeName = _myDmsEntity.ShelfLife.FirstOrDefault(s => s.ShelfLife_Code == d.ShelfLife_Code).ShelfLife_Name,
                            d.Updation_Date
                        };
            DataTable table = query.CopyToDataTable();
            DataView view = table.DefaultView;
            gvDocuments.DataSource = view;
            view.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            gvDocuments.DataBind();
        }


        protected void gvDocuments_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gvDocuments.PageIndex = e.NewPageIndex;
        }



        protected void btnDel_Click(object sender, EventArgs e)
        {
            int selectedCount = gvDocuments.SelectedRowIndexArray.Length;
            if (selectedCount > 0)
            {
                for (int i = 0; i < selectedCount; i++)
                {
                    int rowIndex = gvDocuments.SelectedRowIndexArray[i];
                    var docCode = Convert.ToInt32(gvDocuments.DataKeys[rowIndex][0]);
                    var selectDoc =
                        _myDmsEntity.Document.FirstOrDefault(
                            d => d.Document_Code == docCode);
                    if (selectDoc.Owner != Author.user_Code)
                    {
                        Alert.ShowInTop("你不是文档“" + selectDoc.Document_Name + "”的负责人，无权删除。");
                    }
                    else
                    {
                        _myDmsEntity.Document.DeleteObject(selectDoc);
                        _myDmsEntity.SaveChanges();
                    }
                }
            }
            BindgvDocuments();
        }
        protected void btnView_Click(object sender, EventArgs e)
        {
            int rowIndex = gvDocuments.SelectedRowIndexArray[0];
            var docCode = Convert.ToInt32(gvDocuments.DataKeys[rowIndex][0]);
                    var selectDoc =
                        _myDmsEntity.Document.FirstOrDefault(
                            d => d.Document_Code == docCode);
            PageContext.RegisterStartupScript(
                    DocEditorWindow.GetShowReference(
                        string.Format("DocEditor/DocEditor.aspx?dept={0}&proj={1}&task={2}&folder={3}&id={4}&doctype={5}&act={6}", _dept_code,
                                      _proj_code, _task_code, _folder_code, docCode, selectDoc.File_Path.Split('.').Last().ToLower(),"open")));
        }
        protected void btnPrint_Click(object sender, EventArgs e)
        {
            int rowIndex = gvDocuments.SelectedRowIndexArray[0];
            var docCode = Convert.ToInt32(gvDocuments.DataKeys[rowIndex][0]);
            var selectDoc =
                _myDmsEntity.Document.FirstOrDefault(
                    d => d.Document_Code == docCode);
            PageContext.RegisterStartupScript(
                    DocEditorWindow.GetShowReference(
                        string.Format("DocEditor/DocEditor.aspx?dept={0}&proj={1}&task={2}&folder={3}&id={4}&doctype={5}&act={6}", _dept_code,
                                      _proj_code, _task_code, _folder_code, docCode, selectDoc.File_Path.Split('.').Last().ToLower(), "print")));
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            ExtAspNet.PageContext.Refresh();
        }



    }


}